<template>
  <div class="order-detail">
    <div class="content-box">
      <div class="group-box" v-if="state.orderData && state.orderData.id">
        <locationItem :orderData="state.orderData" :hasMap="state.orderData && state.orderData.status === 7"></locationItem>
      </div>

      <div class="group-box cake-box">
        <div class="title">
          <span class="van-ellipsis w-240">{{`${state.orderData.orderCityName || ''}熊猫不走蛋糕`}}</span>
          <span class="status" :style="getStatusStyle(state.orderData)">{{getStatusText(state.orderData)}}</span>
        </div>

        <template v-if="state.orderData.orderDtls && state.orderData.orderDtls.length">
          <div v-for="(item, index) in state.orderData.orderDtls" :key="index" class="cake-info-box">
            <div class="cake-info" :key="index">
              <van-image class="img" :src="item.mainPic" alt=""></van-image>

              <div class="cake-info-content">
                <div class="row mb-10">
                  <span class="name van-ellipsis"
                    >{{item.name}}</span
                  >
                  <span class="num">x {{item.num}}</span>
                  <!-- <span class="item-price fz-14 fw-bold">¥ {{item.totalPriceYuan}}</span> -->
                </div>

                <div class="row">
                  <span class="remark"
                    >{{item.description}}</span
                  >
                </div>
              </div>
            </div>
            <div v-if="item.blessings" class="cake-info-blessings">
              <span class="label">祝福语</span>
              <span class="value">{{item.blessings}}</span>
            </div>
          </div>
        </template>

        <!-- <div class="discount-info fz-12">
          <span class="c-999">优惠券</span>
          <span class="c-red">- ¥5.00</span>
        </div> -->

        <div class="summary-info">
          <span class="c-333">共{{state.orderData.orderDtls ? state.orderData.orderDtls.length : 0}}件商品
            <!-- 实付: <span class="fz-16 fw-bold">¥ {{state.orderData.totalPriceYuan}}</span> --></span>
        </div>
      </div>

      <div class="group-box order-box mb-10">
        <van-cell-group>
          <van-cell title="订单号" :value="state.orderData.xmbzNo" />
          <van-cell title="下单时间" :value="state.orderData.orderTimeStr" />
          <van-cell title="配送方式" value="送货上门" />
          <van-cell class="d-f delivery-time-value" title="配送时间">
            <template #value>
              <span>{{getStartEndSpliceTime(state.orderData.startTime, state.orderData.endTime)}}</span>
            </template>
          </van-cell>
          <van-cell title="配送员" :value="state.orderData.delivery" />
          <van-cell title="联系电话" :value="state.orderData.deliveryPhone">
            <div class="phone">
              {{state.orderData.deliveryPhone}}
              <span v-if="state.orderData.deliveryPhone" type="danger" class="call" @click="onCall">拨打</span>
            </div>
          </van-cell>
        </van-cell-group>
      </div>

      <div class="group-box">
        <van-cell class="mb-10" title="买家留言" :value="state.orderData.createTime" />
      </div>

      <div class="group-box contact-box fz-14">
        <span @click="onEdit" v-if="canEdit(state.orderData)"><van-icon name="edit" />修改信息</span>
        <!-- <span class="line"></span>
        <span @click="onContactCall"><van-icon name="phone-o" />联系商家</span> -->
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, reactive, ref, onMounted } from 'vue';
import locationItem from './components/locationItem.vue'
import { orderDetail } from '@/api/common'
import { useRoute, useRouter } from 'vue-router';
import { getStatusText, getStatusStyle } from '@/utils/order'
import { getStartEndSpliceTime } from '@/utils/time'
import { canEdit } from '@/utils/order';
export default defineComponent({
  name: 'OrderDetail',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const orderId = route.query.id || ''
    const state = reactive({
      orderData: {}
    })
    const onCall = () => {
      window.location.href = `tel://${state.orderData.deliveryPhone}`
    }
    const onContactCall = () => {
      window.location.href = `tel://952014`
    }
    const getOrderData = () => {
      const params = {
        id: orderId
      }
      orderDetail(params).then(res => {
        state.orderData = res.data || {}
        if (state.orderData) {
          state.orderData.totalPriceYuan = (state.orderData.totalPrice / 100).toFixed(2);
        }
        if (state.orderData.orderDtls && state.orderData.orderDtls.length) {
          state.orderData.orderDtls.forEach(item => {
            item.totalPriceYuan = (item.totalPrice / 100).toFixed(2);
          })
        }
      })
    }
    getOrderData()
    const onEdit = () => {
      router.push({
        path: '/order/edit',
        query: {id: state.orderData.id, from: 'detail'}
      })
    }

    return {
      state,
      onCall,
      onContactCall,
      getStatusText,
      getStatusStyle,
      getStartEndSpliceTime,
      canEdit,
      onEdit,
    }
  },
  components: {
    locationItem,
  }
})
</script>

<style lang="less" scoped>
.order-detail {
  min-height: 100vh;
  background: #f6f8f7;
  .content-box {
   padding: 10px;
  }
  .cake-box {
    padding: 10px;
    background: #fff;
    border-radius: 10px;
    margin-bottom: 10px;
    .title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .status {
        font-size: 12px;
      }
    }
    .cake-info-box {
      padding: 12px 0;
      margin: 12px 0;
      border-top: 1px solid #f9f8f8;
      border-bottom: 1px solid #f9f8f8;
      .cake-info {
        display: flex;
        .img {
          width: 70px;
          height: 70px;
          margin-right: 8px;
        }
        &-content {
          flex: 1;
          .row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .num {
              display: inline-block;
            }
            .item-price {
              display: inline-block;
            }
          }
          .remark {
            font-size: 12px;
            display: inline-block;
            width: 180px;
            color: #999;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }
          .price {
            font-size: 14px;
          }
          .name {
            display: inline-block;
            width: 160px;
            font-size: 14px;
          }
          .num {
            color: #756e6e;
            font-size: 14px;
          }
        }
      }
      .cake-info-blessings {
        font-size: 12px;
        padding: 6px 8px;
        margin-top: 8px;
        border-radius: 4px;
        display: flex;
        .label {
          display: inline-block;
          width: 62px;
          margin-right: 8px;
          text-align: center;
        }
        .value {
          color: #D61718;
        }
        background-color: #fef8f8;
      }
    }
    .discount-info {
      display: flex;
      justify-content: space-between;
      padding-bottom: 12px;
      margin-bottom: 12px;
      border-bottom: 1px solid #f9f8f8;
    }
    .summary-info {
      font-size: 12px;
      text-align: right;
    }
  }
  .order-box {
    .phone {
      display: flex;
      align-items: center;
      justify-content: right;
      .call {
        font-size: 12px;
        margin-left: 10px;
        padding: 0 16px;
        border-radius: 20px;
        color: #D61718;
        border: 1px solid #D61718;
        line-height: 24px;
      }
    }
    .delivery-time-value {
      :deep(.van-cell__value) {
        flex: 2;
      }
    }
  }
  .contact-box {
    background: #fff;
    color: #1C75BC;
    line-height: 48px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    span {
      text-align: center;
      display: inline-block;
      width: 50%;
    }
    .line {
      background: #efefef;
      display: inline-block;
      width: 2px;
      height: 20px;
    }
  }
}
.group-box {
  background: #fff;
  border-radius: 6px;
  margin-bottom: 10px;
  .van-cell-group {
    background: transparent;
  }
  .van-cell {
    background: transparent;
  }
  .location-map {
    box-sizing: border-box;
    border-radius: 2px;
  }
}
</style>
